#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
//int int_cmp(const void* p1,const void* p2)
//{
//	return (*(int*)p1 - *(int*)p2);
//}
//
//int main ()
//{
//	int arr[] = {3,4,8,2,9,4,2,6,2};
//	int sz = sizeof(arr) / sizeof(arr[0]);
//	qsort(arr, sz, sizeof(int), int_cmp);
//	return 0;
//}
//struct stu
//{
//	char name[20];
//	int age;
//};
//int int_cmp_name( void* p1, void* p2)
//{
//	return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);
//}
//int int_cmp_age(void* p1, void* p2)
//{
//	return ((struct stu*)p1)->age-((struct stu*)p2)->age;
//}
//void test1()
//{
//	struct stu s[] = { {"zhangsan",16},{"lisi",23},{"wangwu",20} };
//	int sz = sizeof(s) / sizeof(s[0]);
//	qsort(s, sz, sizeof(s[0]), int_cmp_name);
//}
//void test2()
//{
//	struct stu s[] = { {"zhangsan",16},{"lisi",23},{"wangwu",20} };
//	int sz = sizeof(s) / sizeof(s[0]);
//	qsort(s, sz, sizeof(s[0]), int_cmp_age);
//}
//int main()
//{
//	//test1();
//	test2();
//	return 0;
//}
int int_cmp(const void* p1, const void* p2)
{
	return (*(int*)p1 - *(int*)p2);
}
void swap(char* buf1, char* buf2,int width)
{
	int i = 0;
	for (i = 0;i < width;i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}
void my_sort(void* base, int sz,int width,int(*cmp)(const void* p1,const void* p2 ))
{
	int tmp = 0;
	int i = 0;
	int j = 0;
	int flag = 1;
	for (i = 0;i < sz - 1;i++)
	{
		for (j = 0;j < sz - i - 1;j++)
		{
			if (cmp((char*)base+j*width,(char*)base+(j+1)*width)>0)
			{
				swap((char*)base + j * width, (char*)base + (j + 1) * width,width);
				flag = 0;
			}
		}
		if (flag == 1)
		{
			return;
		}
	}
}

int main()
{
	int arr[] = { 4,7,2,4,2,7,1,9 };
	int sz = sizeof(arr) / sizeof(arr[0]) - 1;
	my_sort(arr,sz,sizeof(arr[0]),int_cmp);
	for (int i = 0;i < sz;i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}